www.gusucode.com > 基于控制台的VC++ 图书管理系统-源码程序 > 基于控制台的VC++ 图书管理系统-源码程序/code/图书管理系统/Get.cpp

    //Download by http://www.NewXing.com
//***************************************************************************
//                  在获取书的信息的时候加以限制的函数
//***************************************************************************

#include "First.h"

//使输入的字符串合法

void GetString(int limit,char str[])
{
	char key;                                   //由键盘获得的字符
	int count=0;                                //计数

	do{
		key=getch();
		if ((key==8)&&(count!=0))				//对Backspace键操作
		{
			count--;
			cerr<<"\b \b";
		}
    if (key>=32 && key<=126 && count<limit)     //在限制范围内输入并显示常用字符
		{
			cerr << key;
			str[count++]=toupper(key);
		}
	}while (!(key==13 && count<=limit && count>0));

	cout << endl;
	str[count]='\0';                             //在字符串末尾补零
}

//使输入的ISBN号合法

void GetInt(int limit,int less,char str[])
{
	char key;
	int count=0;

	do{
		key=getch();
		if (key==8 && count!=0)
		{
			count--;
			cerr << "\b \b";
		}
		if (key>='0' && key<='9' && count<limit)  //在限制范围内输入并显示数字
		{
			cerr << key;
			str[count++]=key;
		}
	}while (!(key==13 && count<=limit && count>=less));

	cout << endl;
	str[count]='\0';
}

//插入字符

void strInsert(char str[],int in,char insert)
{
	int length,                  //字符长度
		index;                   //插入位置
	length=strlen(str);
	for (index=length;index>=in;index--)
		str[index+1]=str[index];
	str[in]=insert;              //将要插入的字符放入指定位置
}  

//格式化读取日期:****-**-**

void GetDate(char str[])
{
	int index=0,dash=0;			 //总共已输的数,横杠数
	char key=0;					 //由键盘获得的字符
	int number[3]={0};			 //已输的数
	const limit[3]={4,2,2};		 //限制

	while (!(key==13 && number[2]>0))
	{
		key=getch();			
		if (key==8 && index!=0)
		{
			index--;
			cerr << "\b \b";
			if (number[dash]>0)
				number[dash]--;
			else
				dash--;			
		}
		if ((key>='0' && key<='9') && number[dash]<limit[dash])
		{
			str[index]=key;
			index++;
			number[dash]++;
			cerr << key;
		}
		if (key=='-' && dash<2 && number[dash]>0)   //输入"-"
		{
			dash++;
			str[index]=key;
			index++;
			cerr << key;
		}
	}
	cout << endl;
	str[index]='\0';

	while (number[0]!=4)			//转化成****-**-**的格式
	{
		strInsert(str,0,'0');
		number[0]++;
	}
	if (number[1]!=2)
		strInsert(str,5,'0');
	if (number[2]!=2)
		strInsert(str,8,'0');
}

//判断日期是否合法

bool boolDate(char str[])
{
	int year,month,day;
	const dayOfMonth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
 
    //把字符型转化成整型,读到数字完为止转化
	year=atoi(str);       
	month=atoi(str+5);
	day=atoi(str+8);

	if (month>12 || month<1)
	{
		cout << "月份错误!\n";
		return false;
	}
	if (!((year%4==0 && year%100!=0) || year%400==0) && month==2 && day==29)
	{
		cout << "平年的二月无29日!\n";
		return false;
	}
	if (day>dayOfMonth[month-1] || day<1) 
	{
		cout << "该月无" << day << "日!\n";
		return false;
	}
	return true;
}

//读取一个浮点数

void GetFloat(char string[])
{
	int index=0,dash=0;		//总共输的数,小数点
	char key=0;             //由键盘获得的字符
	int number[2]={0};		//小数点后的数位
	const limit[2]={4,2};	//限制

	while (!(key==13 && ((dash>0 && number[1]>0) || dash==0)))
	{
		key=getch();	
		
		if (key==8 && index!=0)
		{
			index--;
			cerr << "\b \b";
			if (number[dash]>0)
				number[dash]--;
			else
				dash--;			
		}

		if ((key>='0' && key<='9') && number[dash]<limit[dash])
		{
			string[index]=key;
			index++;
			number[dash]++;
			cerr << key;
		}

		if (key=='.' && dash==0)                  //输入"."
		{
			dash++;
			string[index]=key;
			index++;
			cerr << key;
		}
	}
	cout << endl;
	string[index]='\0';
}